package com.mj.app.health.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mj.app.health.R;


/**
 *
 * Created by haohe on 2017/6/16 0016.
 */

public class DialogLoading {

    LVCircularRing mLoadingView;
    Dialog mLoadingDialog;
    private Context context;
    private String msg;

    public DialogLoading(Context context, String msg){
        this(context, msg, true, false);
        this.context = context;
        this.msg = msg;
    }

    public DialogLoading(Context context, String msg, boolean cancelAble, boolean cancelOnTouchOutside) {
        this.context = context;
        this.msg = msg;
        // 首先得到整个View
        View view = LayoutInflater.from(context).inflate(
                R.layout.dialog_loading, null);
        // 获取整个布局
        LinearLayout layout = (LinearLayout) view.findViewById(R.id.dialog_view);
        // 页面中的LoadingView
        mLoadingView = (LVCircularRing) view.findViewById(R.id.lv_circularring);
        // 页面中显示文本
        TextView loadingText = (TextView) view.findViewById(R.id.loading_text);
        // 显示文本
        loadingText.setText(msg);
        // 创建自定义样式的Dialog
        mLoadingDialog = new Dialog(context, R.style.loading_dialog);
        // 设置返回键无效
        mLoadingDialog.setCancelable(cancelAble);
        mLoadingDialog.setCanceledOnTouchOutside(cancelOnTouchOutside);
        mLoadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
    }

    public void show(){
        if(mLoadingDialog == null){
            new DialogLoading(context, msg);
        }
        if(!mLoadingDialog.isShowing()){

            mLoadingDialog.show();
            mLoadingView.startAnim();
        }
    }

    public void close(){
        if (mLoadingDialog!=null && mLoadingDialog.isShowing()) {
            mLoadingView.stopAnim();
            mLoadingDialog.dismiss();
//            mLoadingDialog=null;
        }
    }
}
